import config from '@/config'

export default {
	data() {
		return {
			shareOpts: {
				title: `邀您浏览${config.mallName}商城~!`,
				path: `/pages/mall/mall`,
				imageUrl: 'https://cwmall.oss-cn-shanghai.aliyuncs.com/mp-icon/qjlogo.png',
				desc: '',
				content: ''
			}
		}
	},
	onLoad(opts) {
		uni.showShareMenu({
			menus: ['shareAppMessage', 'shareTimeline']
		})
		
		// 通过二维码扫进来的, 不处理
		const { scene, m } = opts

		this.$messageCenter.subscribe('userinfo_added', userInfo => {
			const { isHasAgentor } = userInfo || {}
			!scene
				&& m
				&& isHasAgentor !== '1'
				&& setTimeout(this.__bind_customer_20210531.bind(this, m), 1500)
		})
		
	},
	methods: {
		async __bind_customer_20210531(customerId) {
			const { success, data } = await this.$api.getCustomerInfo(customerId)
			if (success && data) {
				this.$store.dispatch('UPDATE_MALL_OWNER', data)
				uni.showModal({
					title: `欢迎光临 ${data.nickName} 的商城`,
					content: '登录后才能享受商城的各项福利哦~!',
					confirmText: '去登录',
					showCancel: false,
					success: () => {
						this.$store.dispatch("USER_LOGIN", {
							desc: "用于登录时记录您的必要身份信息",
							callBack: async (userInfo) => {
								await this.$api.bindAgentor(this.$store.state.customerId)
								// console.log(userInfo, 'userInfo')
							}
						})
					}
				})
			}
		}
	},
	onShareAppMessage() {
		const { getters: { user = {} } = {}, state: { customerId = '' } = {} } = this.$store || {}
		const { path } = this.shareOpts
		let _path = path
		
		if (user.islogin && user.role === 2) {
			_path = `${path}${path.indexOf('?') > -1 ? '&' : '?'}m=${customerId}`
		}
		
		console.log(_path, '-path')

		return {
			title: this.shareOpts.title,
			path: _path,
			imageUrl: this.shareOpts.imageUrl,
			desc: this.shareOpts.desc,
			content: this.shareOpts.content,
			success(res){
				uni.showToast({
					title:'分享成功'
				})
			},
			fail(res){
				uni.showToast({
					title:'分享失败',
					icon:'none'
				})
			}
		}
	},
	onShareTimeline() {
		const { getters: { user = {} } = {}, state: { customerId = '' } = {} } = this.$store || {}
		
		const ret = {
			title: this.shareOpts.title,
			imageUrl: this.shareOpts.imageUrl,
		}
		
		if (user.islogin && user.role === 2 && customerId) {
			query = `m=${customerId}`
		}

		return ret
	}
}